<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="受邀人主键" prop="topartnersid">
        <el-input
          v-model="queryParams.topartnersid"
          placeholder="请输入受邀人主键"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="邀请人主键" prop="frompartnersid">
        <el-input
          v-model="queryParams.frompartnersid"
          placeholder="请输入邀请人主键"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="受邀人姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入受邀人姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="受邀人手机号码" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入受邀人手机号码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="受邀人地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入受邀人地址"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="受邀人账号" prop="useraccount">
        <el-input
          v-model="queryParams.useraccount"
          placeholder="请输入受邀人账号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="受邀人密码" prop="password">
        <el-input
          v-model="queryParams.password"
          placeholder="请输入受邀人密码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="受邀人用户ID" prop="userid">
        <el-input
          v-model="queryParams.userid"
          placeholder="请输入受邀人用户ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="获得积分" prop="accpoints">
        <el-input
          v-model="queryParams.accpoints"
          placeholder="请输入获得积分"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="处理状态：1.审核中 2.通过 3.驳回" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择处理状态：1.审核中 2.通过 3.驳回" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否删除：1.未删除 2.已删除" prop="isdel">
        <el-input
          v-model="queryParams.isdel"
          placeholder="请输入是否删除：1.未删除 2.已删除"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="邀请时间" prop="ctime">
        <el-date-picker clearable size="small"
          v-model="queryParams.ctime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择邀请时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="审核时间" prop="utime">
        <el-date-picker clearable size="small"
          v-model="queryParams.utime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择审核时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['audit:invite:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['audit:invite:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['audit:invite:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['audit:invite:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="inviteList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="邀请ID" align="center" prop="inviteid" />
      <el-table-column label="受邀人主键" align="center" prop="topartnersid" />
      <el-table-column label="邀请人主键" align="center" prop="frompartnersid" />
      <el-table-column label="受邀人姓名" align="center" prop="name" />
      <el-table-column label="受邀人手机号码" align="center" prop="phone" />
      <el-table-column label="受邀人地址" align="center" prop="address" />
      <el-table-column label="受邀人账号" align="center" prop="useraccount" />
      <el-table-column label="受邀人密码" align="center" prop="password" />
      <el-table-column label="受邀人申请理由" align="center" prop="remark" />
      <el-table-column label="受邀人用户ID" align="center" prop="userid" />
      <el-table-column label="获得积分" align="center" prop="accpoints" />
      <el-table-column label="处理状态：1.审核中 2.通过 3.驳回" align="center" prop="status" />
      <el-table-column label="是否删除：1.未删除 2.已删除" align="center" prop="isdel" />
      <el-table-column label="邀请时间" align="center" prop="ctime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.ctime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核时间" align="center" prop="utime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.utime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['audit:invite:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['audit:invite:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改合伙人邀请对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="受邀人主键" prop="topartnersid">
          <el-input v-model="form.topartnersid" placeholder="请输入受邀人主键" />
        </el-form-item>
        <el-form-item label="邀请人主键" prop="frompartnersid">
          <el-input v-model="form.frompartnersid" placeholder="请输入邀请人主键" />
        </el-form-item>
        <el-form-item label="受邀人姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入受邀人姓名" />
        </el-form-item>
        <el-form-item label="受邀人手机号码" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入受邀人手机号码" />
        </el-form-item>
        <el-form-item label="受邀人地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入受邀人地址" />
        </el-form-item>
        <el-form-item label="受邀人账号" prop="useraccount">
          <el-input v-model="form.useraccount" placeholder="请输入受邀人账号" />
        </el-form-item>
        <el-form-item label="受邀人密码" prop="password">
          <el-input v-model="form.password" placeholder="请输入受邀人密码" />
        </el-form-item>
        <el-form-item label="受邀人申请理由" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入受邀人申请理由" />
        </el-form-item>
        <el-form-item label="受邀人用户ID" prop="userid">
          <el-input v-model="form.userid" placeholder="请输入受邀人用户ID" />
        </el-form-item>
        <el-form-item label="获得积分" prop="accpoints">
          <el-input v-model="form.accpoints" placeholder="请输入获得积分" />
        </el-form-item>
        <el-form-item label="处理状态：1.审核中 2.通过 3.驳回">
          <el-radio-group v-model="form.status">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否删除：1.未删除 2.已删除" prop="isdel">
          <el-input v-model="form.isdel" placeholder="请输入是否删除：1.未删除 2.已删除" />
        </el-form-item>
        <el-form-item label="邀请时间" prop="ctime">
          <el-date-picker clearable size="small"
            v-model="form.ctime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择邀请时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="审核时间" prop="utime">
          <el-date-picker clearable size="small"
            v-model="form.utime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择审核时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInvite, getInvite, delInvite, addInvite, updateInvite } from "@/api/audit/invite";

export default {
  name: "Invite",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 合伙人邀请表格数据
      inviteList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        topartnersid: null,
        frompartnersid: null,
        name: null,
        phone: null,
        address: null,
        useraccount: null,
        password: null,
        userid: null,
        accpoints: null,
        status: null,
        isdel: null,
        ctime: null,
        utime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "受邀人姓名不能为空", trigger: "blur" }
        ],
        phone: [
          { required: true, message: "受邀人手机号码不能为空", trigger: "blur" }
        ],
        address: [
          { required: true, message: "受邀人地址不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询合伙人邀请列表 */
    getList() {
      this.loading = true;
      listInvite(this.queryParams).then(response => {
        this.inviteList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        inviteid: null,
        topartnersid: null,
        frompartnersid: null,
        name: null,
        phone: null,
        address: null,
        useraccount: null,
        password: null,
        remark: null,
        userid: null,
        accpoints: null,
        status: 0,
        isdel: null,
        ctime: null,
        utime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.inviteid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加合伙人邀请";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const inviteid = row.inviteid || this.ids
      getInvite(inviteid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改合伙人邀请";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.inviteid != null) {
            updateInvite(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInvite(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const inviteids = row.inviteid || this.ids;
      this.$confirm('是否确认删除合伙人邀请编号为"' + inviteids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delInvite(inviteids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('audit/invite/export', {
        ...this.queryParams
      }, `audit_invite.xlsx`)
    }
  }
};
</script>